package com.bruan.controller;

import com.bruan.anno.MyLog;
import com.bruan.pojo.Emp;
import com.bruan.pojo.Result;
import com.bruan.service.EmpService;
import com.bruan.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    //注入
    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
      log.info("员工登录信息：{}",emp);
      Emp e =empService.login(emp);
      //登陆成功 生成令牌
        if(e != null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("username",e.getUsername());
            String jwt = JwtUtils.generateJwt(claims);//包含当前登录的员工信息
            return Result.success(jwt);
        }
        else {
            return Result.error("员工信息不存在");
        }
    }
}
